import axios from 'axios'
import qs from 'qs'

const baseUrl = process.env.VUE_APP_BASE_URL
let httpRequest = axios.create({
    timeout:10000,
    baseURL:baseUrl,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})

httpRequest.interceptors.request.use((config) => {
    if(config.method  === 'post'){
        config.data = qs.stringify(config.data);
    }
    return config;
},(error) =>{
    return Promise.reject(error);
});

httpRequest.interceptors.response.use((res) =>{
    alert(22)
    if (res.status != 200){
        alert('错误')
        return Promise.reject('error')
    }
    if (res.data.status == 0){
        alert(res.data.msg)
        return Promise.reject(res.data.msg)
    }
    if(!res.data.success){
        return Promise.resolve(res);
    }
    return res;
}, (error) => {
    alert('网络开小差了！！！')
    return Promise.reject(error);
});


export default httpRequest